﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace Model
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	internal class CmdPathwayCheck : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			this.m_uiApp = cmdData.Application;
			this.m_uiDoc = this.m_uiApp.ActiveUIDocument;
			this.m_app = this.m_uiApp.Application;
			this.m_doc = this.m_uiDoc.Document;
			this.m_sel = this.m_uiDoc.Selection;
			try
			{
				if ((int)this.m_doc.ActiveView.ViewType != 4)
				{
					Result result = SwitchView.SetUsableView(HYView.CmdViewType.ThreeD, cmdData);
					if (result != Result.Succeeded)
					{
						return result;
					}
				}
				RevitVersionFuncs.ClearSelection(this.m_sel);
				Reference reference = this.m_sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new RevitFilter.SelectionCableTrayConduitFilter(), "请选择桥架或线管");
				CableTrayConduitBase device = this.m_doc.GetElement(reference) as CableTrayConduitBase;
				IList<Element> pathwayDevices = new PathwayCheck().GetPathwayDevices(device, SearchDeviceType.CableTray | SearchDeviceType.Conduit | SearchDeviceType.CableTrayFitting | SearchDeviceType.ConduitFitting | SearchDeviceType.Device);
				if (pathwayDevices.Count > 0)
				{
					RevitVersionFuncs.SetSelection(this.m_sel, pathwayDevices.ToList<Element>());
				}
			}
			catch (Exception)
			{
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		protected UIApplication m_uiApp;

		protected UIDocument m_uiDoc;

		protected Application m_app;

		protected Document m_doc;

		protected Selection m_sel;
	}
}
